<link rel="stylesheet" type="text/css" href="<?=base_url()?>file/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<?=base_url()?>file/js/easyui/themes/gray/easyui.css">

  <div class="page-title">
    <h2><?=$title?></h2>
  </div>
<div class="box">


<table class="general alignleft">
  <thead>
    <tr>
      <th>Territory</th>
      <th><?php echo $region.', '.$cluster;?></th>
    </tr>
  </thead>

  <tr>
    <td>Smartfren Gadget Ambassador</td>
    <td>
      <select name="sales_id" id="sales-id">
        <option value="">Pilih SGA</option>
        <?php foreach ($sga as $key) : ?>
            <option value="<?php echo $key->user_group_id.'_'.$key->user_id; ?>" ><?php echo $key->user_id.' | '.$key->user_name; ?></option>
        <?php endforeach; ?>
      </select>
    </td>
  </tr>
  <tr>
    <td>Tipe JC</td>
    <td>
      <select name="selling_type" id="selling-type">
        <option value="1" selected="selected">Kunjungan Outlet</option>
        <option value="2">Mobile Selling</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>Lokasi Mobsell</td>
    <td>
      <select name="agent_type" id="agent-type">
        <?php 
          foreach ($agent_status as $key) {
            echo '<option value="'.$key->member_value.'">'.$key->member_display.'</option>';
          }
        ?>
      </select>
    </td>
  </tr>
  <tr>
    <td>Bulan Referensi</td>
    <td>
      <select name="month" id="month" class="d_jc_sfa">
      <?php 
        foreach ($months as $key ) {
          if (date('Ym') == $key[0]) 
            echo '<option value="'.$key[0].'" selected="selected">'.$key[1].'</option>';
          else
            echo '<option value="'.$key[0].'">'.$key[1].'</option>';
        }
      ?>
      </select>
    </td>
  </tr>
  <tr>
    <td></td>
    <td>
      <div id="button">
        <button id="show-jc">Show JC</button>
      </div>
    </td>
  </tr>
</table>


  <div id="jc-table">

  </div>

  <div id="populate">
    <img src="<?=base_url()?>file/images/spinner.gif"> Populate Journey Cycle
  </div>

</div> <!-- Box -->

<script type="text/javascript">
  $(document).ready(function() {

    $('#populate').hide(); //$('#pesan').hide();
    $("#show-jc").click(function(event){
      if( $('#sales-id').val() == '' || $('#sales-id').val() == null || $('#selling-type').val() == 0 ) 
        { 
          event.preventDefault();
          alert('Nama Sales \n tidak boleh kosong'); 
        }
      else 
      {
        var sales_id     = $('#sales-id').val().split('_');
        var agent_type   = $('#agent-type').val();
        var selling_type = $('#selling-type').val();
        var month        = $('#month').val();

        event.preventDefault();
        $('#jc-table').empty(); $('#populate').show();
        $.post(
          // '<?=base_url()?>index.php/jc/journey/sfa',
          '<?=base_url()?>index.php/jc/load_journey',
          {sales_id:sales_id[1],user_group_id:sales_id[0],agent_type:agent_type,selling_type:selling_type,month_work:month},
          function(data) {
            if(data.channels_maintained == 0 ) {
              $('#jc-table').show(); $('#populate').hide();
              $('#jc-table').html('Tidak ada Outlet yang dikelola');
            }
            else {
              $('#jc-table').show(); $('#populate').hide();
              $('#jc-table').html(data.journey_summary);
              
            }
          },"json"
        ).error(function() {
          $('#jc-table').show(); $('#populate').hide();
          $('#jc-table').html('Wrong Format');
        });
      }
    });
  });
  
function simpan (param) {
    var id_attr      = param.split('_');
    var channel_id   = id_attr[0];
    var territory_id = id_attr[1];
    var yyyymm       = id_attr[2];
    var day          = id_attr[3];
    var sales_id     = $('#sales-id').val().split('_');
    var nilai        = $(':checkbox#'+param).is(':checked');

    $.post(
      '<?=base_url()?>index.php/jc/save_jc/35', 
      {user_id:sales_id[1],channel_id:channel_id,territory_id:territory_id,month:yyyymm,day:day,nilai:nilai},
      function(data) {
        $(".pesan").html(data.messege).fadeIn(1000).fadeOut(1000);
      },
      "json"
    );
}
</script>